package com.yin.dubboconsumer;

import java.math.BigDecimal;

/**
 * 股票相关计算
 *
 * @author yinguilong
 * @Time 9/9/21 10:01 AM
 **/
public class StockMath {
    /**
     * 获取n年以后财富增长
     *
     * @param originMoney
     * @param years
     * @param rate
     * @return
     */
    public static BigDecimal getNYearsMoney(BigDecimal originMoney, int years, BigDecimal rate) {
        for (int i = 0; i < years; i++) {
            originMoney = originMoney.multiply(rate.add(BigDecimal.ONE));
        }
        return originMoney.setScale(BigDecimal.ROUND_CEILING, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 获取N年后折现现金
     *
     * @param originMoney 当前现金金额
     * @param years       N年
     * @param rate        增长率
     * @param disRate     折现率
     * @return
     */
    public static BigDecimal getNYearsCash(BigDecimal originMoney, int years, BigDecimal rate, BigDecimal disRate) {
        originMoney = getNextYearsProfit(originMoney, years, rate);
        if (disRate.equals(BigDecimal.ZERO)) {
            return originMoney;
        }
        for (int i = 0; i < years; i++) {
            originMoney = originMoney.divide(disRate.add(BigDecimal.ONE), BigDecimal.ROUND_CEILING);
        }
        return originMoney;
    }

    public static BigDecimal getNextYearsProfit(BigDecimal originMoney, int years, BigDecimal rate) {
        for (int i = 0; i < years; i++) {
            originMoney = originMoney.multiply(rate.add(BigDecimal.ONE));
        }
        return originMoney;
    }

    public static BigDecimal getNYearsValue(BigDecimal originMoney, int years, BigDecimal rate, BigDecimal disRate) {
        BigDecimal seedMoney = new BigDecimal(originMoney.toString());
        for (int i = 0; i < years; i++) {
            originMoney = originMoney.add(getNYearsCash(seedMoney, i + 1, rate, disRate));
        }
        return originMoney.setScale(BigDecimal.ROUND_CEILING, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 获取平均增长率
     *
     * @param originMoney
     * @param years
     * @param targetMoney
     * @return
     */
    public static BigDecimal getNYearsAvgRate(BigDecimal originMoney, int years, BigDecimal targetMoney) {
        BigDecimal divid = targetMoney.divide(originMoney, BigDecimal.ROUND_CEILING);
        double math = Math.pow(divid.doubleValue(), 1.0 / years);

        BigDecimal decimal = new BigDecimal(math);
        return decimal.add(new BigDecimal(-1)).setScale(BigDecimal.ROUND_CEILING, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 获取 当前市值 现金流折现规则下的年数
     *
     * @param originMoney
     * @param years
     * @param rate
     * @param disRate
     * @return
     */
    public static Integer getYearsOfValue(BigDecimal originMoney, int years, BigDecimal rate, BigDecimal disRate) {
        BigDecimal seedMoney = new BigDecimal(originMoney.toString());
        for (int i = 0; i < years; i++) {
            originMoney = originMoney.add(getNYearsCash(seedMoney, i + 1, rate, disRate));
        }
        return 0;
        //return originMoney.setScale(BigDecimal.ROUND_CEILING, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 获取达到目标金额的需要的年限
     *
     * @param originMoney
     * @param targetMoney
     * @param rate
     * @return
     */
    public static int getTargetMoneyYears(BigDecimal originMoney, BigDecimal targetMoney, BigDecimal rate) {
        int year = 1;
        while (originMoney.compareTo(targetMoney) < 0) {
            originMoney = originMoney.add(originMoney.multiply(rate));
            year++;
        }
        return year;
    }
}
